// 压缩之后大于2M 重新压缩
let limitSize = 2
// 选择上传图片
export function wxChooseImage(total, sourceType = ['album', 'camera']) {
  console.log(sourceType)
  return new Promise((resolve, reject) => {
    uni.chooseMedia({
      count: total,
      mediaType: ['image'],
      sourceType,
      success: res => {
        console.log(res, 'compressImage')
        resolve(res)
      },
      fail: err => {
        reject(err)
      }
    })
  })
}
// 获取图片信息
export function wxGetImageInfo(files, size, limitSizeNew) {
  limitSizeNew = limitSizeNew ? limitSizeNew : limitSize
  return new Promise((resolve, reject) => {
    uni.getImageInfo({
      src: files.tempFilePath,
      success: res => {
        let query = {
          width: 0,
          height: 0
        }
        let width = res.width
        let height = res.height
        if (getSize(size) > limitSizeNew) {
          let rate = (width < height ? width / height : height / width) / 1.5
          query.width = width * rate
          query.height = height * rate
        } else {
          query.width = width
          query.height = height
        }
        resolve(query)
      },
      fail: err => {
        reject(err)
      }
    })
  })
}
// 把当前画布指定区域的内容导出生成指定大小的图片
export function wxCanvasToTempFilePath(CanvasId, size, cWidth, cHeight,_this, limitSizeNew) {
  limitSizeNew = limitSizeNew ? limitSizeNew : limitSize
  return new Promise((resolve, reject) => {
    uni.canvasToTempFilePath({
      canvasId: CanvasId,
      fileType: 'jpg',
      destWidth: cWidth,
      destHeight: cHeight,
      quality: Number(setImgSize(size, limitSizeNew)),
      success: res => {
        wxGetFileInfo(res.tempFilePath).then(result => {
          if (getSize(result) > limitSizeNew) {
            console.log(res.tempFilePath)
            console.log(result)
            let obj = {
              size: result,
              tempFilePath: res.tempFilePath,
              status: 1
            }
            resolve(obj)
          } else {
            resolve(res)
          }
        })
      },
      fail: err => {
				console.log(err)
				if (err.errMsg.indexOf('create bitmap failed') != -1) {
					reject(err)
				}
      }
    }, _this)
  })
}
// 获取文件大小
export function wxGetFileInfo(tempFilePath) {
  return new Promise((resolve, reject) => {
    wx.getFileSystemManager().getFileInfo({
      filePath: tempFilePath,
      success: result => {
				console.log(result.size, 'result.size')
        resolve(result.size)
      },
      fail: err => {
        reject(err)
      }
    })
  })
}
// 获取图片大小M
export function getSize(size) {
  return Math.ceil(size / 1024 / 1024)
}
// 设置图片压缩质量
export function setImgSize(size, limitSizeNew) {
  return getSize(size) >= limitSizeNew ? 0.9 : 1
}
import { photosUploadOss, getUploadOssUrl } from '@/api/upload'
// 上传图片到服务器
export function  uploadPhotoApi(imgs,filename) {
  return new Promise((resolve, reject) => {
      getUploadOssUrl(filename).then(res => {
      if (res.resultStatus.code === 200) {
        let data = res.resultValue
        let host = data.host
        let newImgurl = imgs.substring(13)
        let pargrm = {
          'success_action_status': '200',
          OSSAccessKeyId: data.accessKeyId,
          policy: data.policy,
          Signature: data.signature,
          key: `${data.prefix}${newImgurl}`,
        }
        photosUploadOss(host, pargrm, imgs).then((res) => {
          if (res.statusCode >= 200 && res.statusCode < 300) {
            resolve(pargrm.key)
          }
        }).catch(() => {
          reject(err)
        })
      }
    })
  })
}